package co.edu.eam.libreria.persistencia;


import java.util.List;



import javax.persistence.EntityManager;
import javax.persistence.Query;

@SuppressWarnings("unchecked")
public class DAO implements IDAOGenerico {
	
	private EntityManager em;

	public DAO(EntityManager em) {
		super();
		this.em = em;
	}
	
	
	public void crear(Object obj){
		em.persist(obj);
	}
	
	
	public void editar(Object obj){
		em.merge(obj);
	}
	
	public void eliminar(Object obj){
		em.remove(obj);
	}
	
	public <T> T buscar(Class<T> clase,Object obj){
		return em.find(clase, obj);
	}
	
	
	public <T> List<T> ejecutarNameQuery(String nombreQuery, Object...param){
		Query q=em.createNamedQuery(nombreQuery);
		for (int i = 0; i < param.length; i+=2) {
			q.setParameter(param[i].toString(), param[i+1]);
		
		}
		return q.getResultList();
	}
	
	

}
